$(document).ready(function()
    {
        $.table_master = null;
        
        $.init_master = function()
        {
            $.ajax({
                url: base_url + 'br-02/jenis_barang/get_all',
                type: 'post',
                success : function(data)
                {
                    $.table_master = $('#jenis_barang_master').dataTable({
                        "bJQueryUI": true,
                        "sPaginationType": "full_numbers",
                        "aaData": data.data,
                        "bDestroy": true,
                        "aoColumns":[
                                
								{'sTitle' : 'primary', 'mDataProp' : 'ID_JENIS_BARANG'},		
								
									{'sTitle' : 'NAMA_JENIS_BARANG', 'mDataProp' : 'NAMA_JENIS_BARANG'},
								
							
                            ],
                        "fnRowCallback": function( nRow, aData, iDisplayIndex )
                            {
                                $('td:last', nRow).html('<div style="text-align:center;"> <ul class="action-buttons clearfix"> \n\
                                                            <li> <a class="button button-gray no-text" href="#" onClick="$.update_jenis_barang_form(\''+ $('td:eq(0)', nRow).html() +'\')"> <span class="pencil"></span> </a> </li> \n\
                                                            <li> <a class="button button-gray no-text" href="#" onClick="$.delete_jenis_barang(\''+ $('td:eq(0)', nRow).html() +'\')"> <span class="bin"></span> </a> </li> \n\
                                                        </ul></div>');
                                return nRow;
                            }
                    });
                },
                error: function(res)
                {
                    alert(res.responseText);
                },
                dataType: 'json'
            });
        }
        $.init_master();

        $.add_jenis_barang = function()
        {
            $.call_ajax('br-02/jenis_barang/add/', "#jenis_barang_form", null);
            $('#dialog-jenis_barang-form').dialog('close');
            $.init_master();
        }

        $.update_jenis_barang = function()
        {
            $.call_ajax('br-02/jenis_barang/update/' + $("#ID_jenis_barang").val(), "#jenis_barang_form", null);
            $('#dialog-jenis_barang-form').dialog('close');
            $.init_master();
        }       

        $.delete_jenis_barang = function(id)
        {
            if(!confirm("Apakah anda yakin akan menghapus data ini ?")) return false;
            $.call_ajax('br-02/jenis_barang/delete/' + id, null, null);
            $.init_master();
        }

        $('#dialog-jenis_barang-form').dialog({
            title: 'Data jenis_barang',
            width: '950px',
            autoOpen:false,
            modal:true,
            resizable:false
        });

        $.update_jenis_barang_form = function(id)
        {
            $.call_ajax('br-02/jenis_barang/get/' + id, null, "#jenis_barang_form");
            $("#update_submit").show();
            $("#add_submit").hide();
            $('#dialog-jenis_barang-form').dialog('open');
        }

        $.add_jenis_barang_form = function()
        {
            $.clear_form("jenis_barang_form");

            $("#add_submit").show();
            $("#update_submit").hide();
            $('#dialog-jenis_barang-form').dialog('open');
        }

        $("#jenis_barang_form").submit(function() {return false;});

        $("#jenis_barang_add").click($.add_jenis_barang_form);
        $("#add_submit").click($.add_jenis_barang);
        $("#update_submit").click($.update_jenis_barang);
        
    });